#ifndef _THREAD_HPP_
#define _THREAD_HPP_

#include <iostream>
#include <string>
#include <pthread.h>
#include <unistd.h>
#include <functional>
#include <sys/types.h>

namespace ThreadModule
{
    using func_t = std::function<void(std::string name)>;
    static int number = 1;
    enum class TSTATUS
    {
        NEW,
        RUNNING,
        STOP
    };

    class Thread
    {
    private:
        static void* Routine(void *args)
        {
            Thread *t = static_cast<Thread*>(args);
            t->_status = TSTATUS::NEW;
            t->_func(t->Name());
            return nullptr;
        }

        void EnableDetach()
        {
            _joinable = false;
        }

    public:
        Thread(func_t func) 
            :_func(func)
            ,_status(TSTATUS::NEW)
            ,_joinable(true)
        {
            _name = "Thread-" + std::to_string(number++);
            _pid = getpid();
        }
        bool Start()
        {
            if(_status != TSTATUS::RUNNING)
            {
                int n = ::pthread_create(&_tid, nullptr,Routine,this);
                if(n != 0)
                    return false;
                return true;
            }
            return false;
        }

        bool Stop()
        {
            if(_status == TSTATUS::RUNNING)
            {
                int n = ::pthread_cancel(_tid);
                if(n != 0)
                    return false;
                return true;
            }
            return false;
        }

        bool Join()
        {
            if(_joinable)
            {
                int n = ::pthread_join(_tid, nullptr);
                if(n != 0)
                    return false;
                return true;
            }
            return false;
        }

        bool IsJoinable() 
        {
            return _joinable;
        }

        std::string Name()
        {
            return _name;
        }

        ~Thread()
        {}
    private:
        std::string _name;
        pthread_t _tid;
        pid_t _pid;
        bool _joinable ;//是否是分离的， 默认不是分离的
        func_t _func;
        TSTATUS _status;
    };

}




#endif